<?php

namespace App\Repositories;

use App\Models\CagetoryModel;
use App\Models\AreaModel;
use App\Models\JobsModel;
class CommonRepository
{
    protected $area_mdl;
    protected $category_model;
    protected $job_model;
    public function __construct()
    {
        $this->area_mdl = new AreaModel();
        $this->category_model = new CagetoryModel();
        $this->job_model = new JobsModel();
    }
    /**zwp
     * 
     * @param type $ids
     * @return type
     */
    public function getCategoryById($ids){
        $data = CagetoryModel::getCategoryByIdRange(["pid"],$ids);
        return $data;
    }
    /**zwp
     * 获取产品名称对应的id，没有这个名称则插入，返回插入后的id
     * @param type $name
     * @return array
     */
    public function getId($name){
        $ids = CagetoryModel::getCategoryByName(["id"], $name);
        $ids = $ids->toArray();
        if(!$ids){
            $ids = CagetoryModel::insertNameGetId($name);
            
        }
        return $ids;
    }
    public function getCategoryByIds($arg,$ids){
        return $this->category_model->whereIn("id",$ids)->get($arg);
    }

    public function getJobs(){
        return $this->job_model->all();
        
    }

        /**
     * 查询所有
     * @return mixed
     */
    public function getAll()
    {
        $result = $this->area_mdl->where('id','>',100000)->orderBy('id','asc')->get(['id','name','level','pid']);
        return $result->isEmpty() ? [] : $result->toArray();
    }

    /**
     * 查询所有省份
     * @return mixed
     */
    public function getProvinces()
    {
        $provices = $this->area_mdl->where('level',1)->get(['id','name']);
        return $provices->isEmpty() ? [] : $provices->toArray();
    }

    /**
     * 查询所有城市
     * @param int $pid 省份ID
     * @return array
     */
    public function getCitys($pid=0)
    {
        $citys = $this->area_mdl->where('pid',$pid)->get(['id','name']);
        return $citys->isEmpty() ? [] : $citys->toArray();
    }

    /**
     * 查询所有区域
     * @param int $cid 城市ID
     * @return array
     */
    public function getAreas($cid=0)
    {
        $areas = $this->area_mdl->where('pid',$cid)->get(['id','name']);
        return $areas->isEmpty() ? [] : $areas->toArray();
    }
    
    public function getAreaNameAndId(){
        $data =  $this->area_mdl->get(["id","merger_name"]);
        return $data->isEmpty()?[]:$data->toArray();
    }
    
}